//
//  BKWelcomeViewController.swift
//  微博
//
//  Created by mac on 16/8/11.
//  Copyright © 2016年 xbk. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class BKWelcomeViewController: UIViewController {
    
    //  MARK: -- 懒加载控件
    //背景图片
    private lazy var bgImageView :UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //头像
    private lazy var headImageView:UIImageView = {
    
        let imageView =  UIImageView(image: UIImage(named: "avatar_default_big"))
        
        imageView.layer.cornerRadius = 45
        imageView.layer.masksToBounds = true
        
        return imageView

    }()
    
    //欢迎信息
    private lazy var messageLabel:UILabel = {
    
        let label = UILabel()
        
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        
        if let avatar_large = BKUserAccountViewModel.shareUserAccount.userAccount?.avatar_large{
        
            self.headImageView.sd_setImageWithURL(NSURL(string: avatar_large), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        if let name = BKUserAccountViewModel.shareUserAccount.userAccount?.name {
        
            label.text = "欢迎回来,\(name)"
        }else{
        
            label.text = "欢迎回来"
        }
        
        return label
    }()
    
    override func loadView() {
        
        view = bgImageView
    }
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        setupUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    
    //执行动画
    private func startAnimation () {
    
        messageLabel.alpha = 0
        headImageView.snp_updateConstraints { (make) -> Void in
            
            make.top.equalTo(view).offset(100)
        }
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: nil)
                })
        }
    }
    
    
    //添加视图,设置约束
    private func setupUI () {
    
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        
        headImageView.snp_updateConstraints { (make) -> Void in
            
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        messageLabel.snp_updateConstraints { (make) -> Void in
            
            make.centerX.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(10)
        }
    }
    

}
